require(["../../conf/config.js"], function() {
	require(["jquery", "swiper", "template", "jquery-cookie", "common"], function($, Swiper, template, jqcookie, com) {
		console.log("加载成功shoppinga");
		let alls = getCookie("userlist");
		let allcc = JSON.parse(alls);
		allcc.forEach(function(item){
			if (item.status == 1){	
				item.asum = 0;
				item.shopcar.forEach((ite)=>{
					item.asum += ite.num;
				})
				$("#nav-li2 a span").html(item.asum);
				$("#cla1").html(item.vipname);
				$("#cla1").mouseenter(function() {
					$(".back").css({
						display: "block"
					})
				})
				$(".back").mouseleave(function() {
					$(".back").css({
						display: "none"
					})
				})

				
			} else {
				$("#cla1").html("亲，请登录");
			}
		})
		$(".back").click(function() {
			allc.forEach((item) => {
				item.status = 0;
				item.asum = 0;
			})
			setCookie("userlist", JSON.stringify(allc), 30);
			window.location.replace("http://localhost:8000/pages/index/index.html");
		})


// 		///////////////////////// 
// 
//判断是否购物车有东西
				let zom = getCookie("userlist");
				let zomm = JSON.parse(zom);
				
				zomm.forEach((item)=>{
					
					
					if (item.shopcar.length == 0) {
						$("#emptycar").css({
							display: "block"
						});
						$("#fullcar").css({
							display: "none"
						});
					} else {
						$("#emptycar").css({
							display: "none"
						});
						$("#fullcar").css({
							display: "block"
						});
						console.log(item.shopcar);
						var htmlsrr = "";
					
						function a1(s, f) {
							$("#fullcar").load("shoppingkuang.html", function() {
								htmlsrr = template("shopping", {
									list: item.shopcar
								})
								$("#fullcar").html(htmlsrr);
								s();
							})
						}
					
						function a2(s, f) { //加载完页面之后进行操作 + - delete					
							// 物品数量--------------------
							let znum;
							
							$("#fullcar dt").click(function() {
								//function change(s,f){
									let ind = $(this).parent().parent().index();									
									item.shopcar[ind].num =znum
									znum = $(this).next().text();
									znum--;
									item.shopcar[ind].num = znum;
									if (znum <= 0) {
										$(this).next().text(0);
									
										 item.shopcar.splice(ind,1 );
									} else {
										$(this).next().text(znum);																			
									}					
									window.location.reload();
									setCookie("userlist",JSON.stringify(zomm),30);
								//	s();
							//	}
								//function lod(s,f){
									//window.location.reload();
									 // 2
								//	f();
							//	}
							//	new Promise(change).then(function(){
							//		return new Promise(lod);
							//	})
							})
							//物品数量+++++++++++++++++++++
							$("#fullcar dd").click(function() {
								let mffa= getCookie("userlist");
								let mff = JSON.parse(mffa);
								let ind = $(this).parent().parent().index();
								item.shopcar[ind].num =znum
								mff.forEach((mf)=>{
									let znum = $(this).prev().text();
									znum++;
									$(this).prev().text(znum);
									mf.shopcar[ind].num = znum;
								})							
								setCookie("userlist",JSON.stringify(mff),30);
								window.location.reload();
								
							})
							///////删除
							$("#delete").click(function(){
								let ind = $(this).parent().parent().index();
								item.shopcar.splice(ind,1);			
								window.location.reload();
								setCookie("userlist",JSON.stringify(zomm),30);
							})
							f();
						}
						new Promise(a1).then(function() {
							return new Promise(a2);
						})
					}
				})
				
		
 


		$("#fixed .fixed1").click(function() {
			//console.log($(this));	
			$(this).animate({
				left: "-190px"
			}, 800, function() {
				$("#fixed .fixed2").animate({
					width: "100%"
				}, 800, function() {
					$("#fixed span").css("display", "block");
				});
			});
		})
		$("#fixed span").click(function() {
			$("#fixed span").css("display", "none");
			$("#fixed .fixed2").animate({
				width: 0
			}, 1000, function() {
				$("#fixed .fixed1").animate({
					left: 0
				}, 1000, function() {});
			});
		})

		// underline
		$("#position dl dd a").mouseenter(function() {
			$(this).css({
				textDecoration: "underline"
			})
		})
		$("#position dl dd a").mouseleave(function() {
			$(this).css({
				textDecoration: "none"
			})
		})



	})
})
